package com.example.liuke.guangguang.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.liuke.guangguang.R;
import com.example.liuke.guangguang.bean.bookbean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuke on 2018/10/18.
 */

public class MyBookAdapter extends BaseQuickAdapter<bookbean, BaseViewHolder> {

    private Context mContext;
    private List<bookbean> mydate = new ArrayList<>();
    private int length;

    public MyBookAdapter(Context context, @LayoutRes int layoutResId, @Nullable List<bookbean> data, int length) {
        super(layoutResId, data);
        mContext = context;
        mydate = data;
        this.length = length;
    }

    @Override
    protected void convert(BaseViewHolder helper, bookbean commentData) {
        if (helper.getLayoutPosition() + 1 > mydate.size() - length) {
            helper.getView(R.id.book).setVisibility(View.INVISIBLE);
            commentData.setVisible(View.INVISIBLE);
            System.out.println("quyulimian:" + "zoule");
        } else {
            Glide.with(mContext).load(commentData.getUrl()).into((ImageView) helper.getView(R.id.bookimage));
            commentData.setVisible(View.VISIBLE);
        }
        if (commentData.getVisible() == View.INVISIBLE) {
            System.out.println("quyulimian:" + "in");
            helper.getView(R.id.book).setVisibility(View.INVISIBLE);
        } else if (commentData.getVisible() == View.VISIBLE) {
            System.out.println("quyulimian:" + "vi");
            helper.getView(R.id.book).setVisibility(View.VISIBLE);
        }
    }

    public void remove(int position) {
        mydate.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mydate.size());
    }

    public void add(int position) {
        bookbean bookbean = new bookbean();
        bookbean.setUrl("http://d.hiphotos.baidu.com/image/h%3D300/sign=615ad02161224f4a4899751339f69044/b3b7d0a20cf431ad184ee53e4736acaf2edd981d.jpg");
        bookbean.setVisible(View.VISIBLE);
        mydate.add(position, bookbean);
        notifyItemInserted(position);
        notifyItemRangeChanged(position, mydate.size());
    }
}